<template>
  <!-- MembershipManage 成员管理 -->
  <PageContainer>
    <view class="container">
      <Header title="成员管理" :isBoxShadow="false" />
      <Paglist v-model="dataList" :isInitApi="false" :parame="parame" @register="register">
        <template v-for="v in dataList" :key="v.id">
          <view style="margin-bottom: 30rpx">
            <MemberItem
              :item="v"
              :show-bottom="true"
              :btn-list="true"
              :showBtns="true"
              @update="update"
            />
          </view>
        </template>
      </Paglist>
      <view v-if="isHandel" class="add-member" @click="addFnc">
        <image
          src="https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/floatIcon/member-manage-img.png"
        />
      </view>
    </view>
  </PageContainer>
</template>

<script lang="ts" setup>
  /**  MembershipManage  成员管理 */
  import PageContainer from '@/components/PageContainer/index.vue';
  import Header from '@/components/Header/index.vue';
  import { computed, provide, reactive, ref } from 'vue';
  import { pageLeadingList } from '@/api/mine';
  import type { IMemberItem } from '@/api/mine/types';
  import MemberItem from '../components/MemberItem.vue';
  import Paglist from '@/components/Paglist/index.vue';
  import { useConList } from '@/components/Paglist/useConList';
  import { navigateTo, onLoad, switchTab } from '@/router';
  import { useUserStoreWithOut } from '@/store/modules/user';
  import { isLogin } from '@/utils/is';
  import { onPageScroll, onShareAppMessage } from '@dcloudio/uni-app';
  import { TabbarPageEnum } from '@/enums/pageEnum';
  import { useAppStoreWithOut } from '@/store/modules/app';
  import { Toast } from '@/utils';
  import { ToastTitleEnum } from '@/enums/publicModuleEnum';
  import { MemberRoleEnum } from '@/enums/memberRoleEnum';
  import { useAppShare } from '@/hooks/core/useAppShare';

  const { setWxSharePath } = useAppShare();
  const { currentCocId, cocInfo, checkIsMyUnit } = useUserStoreWithOut();
  const cocId = computed(() => currentCocId as number);
  const isHandel = computed(
    () =>
      cocInfo.memberRoleType === MemberRoleEnum.PRINCIPAL ||
      cocInfo.memberRoleType === MemberRoleEnum.MANAGER,
  );

  const dataList = ref<IMemberItem[]>([]);
  const parame = reactive({});
  const [register, { refresh }] = useConList();

  provide('api', (obj: any) => {
    return pageLeadingList(obj);
  });

  function __init() {
    refresh?.();
  }

  const update = () => refresh();

  const addFnc = () =>
    navigateTo({
      name: 'membershipEdit',
      options: {
        callback: update,
      },
    });

  onLoad<'MembershipManage'>((op) => {
    isLogin(() => {
      op.shareMemberId ? shareJoin(op.shareMemberId) : __init();
    }, true);
  });

  function shareJoin(shareMemberId: number) {
    const time = 1500;
    if (checkIsMyUnit(shareMemberId)) {
      __init();
    } else {
      Toast(
        {
          title: ToastTitleEnum.NOACCESS,
          success: () => switchTab(TabbarPageEnum.BASE_HOME),
        },
        'error',
        time,
      );
    }
  }

  onPageScroll(() => {});

  const { globalConfig } = useAppStoreWithOut();
  onShareAppMessage(() => ({
    imageUrl: globalConfig?.comShareOptions.shareImg,
    path: setWxSharePath({ name: 'membershipManage', options: { shareMemberId: cocId.value } }),
    title: '成员管理' || globalConfig?.comShareOptions.shareTitle,
  }));
</script>

<style lang="scss" scoped>
  $borderColor: #f5f5f5;
  .add-member {
    width: 108rpx;
    height: 144rpx;
    position: fixed;
    right: 30rpx;
    bottom: 240rpx;
    > image {
      width: 100%;
      height: 100%;
    }
  }
</style>
